import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

///zhudaihao
///2024-1-30
///输入编辑圆角
class EditRounded2 extends StatefulWidget {
  ///输入框提示文本
  final String? hint;

  ///角弧度
  final double? radius;

  ///输入限制
  final List<TextInputFormatter>? inputFormatters;

  ///键盘
  final TextInputType? keyboardType;

  ///编辑输入控制器
  final TextEditingController? controller;

  ///是否显示输入密码显示隐藏图标
  final bool passwordVisible;

  ///右边自定义widget
  final Widget? rightWidget;

  ///高
  final double? height;

  ///宽
  final double? width;

  const EditRounded2(
      {this.hint,
      this.radius,
      this.inputFormatters,
      this.keyboardType,
      this.controller,
      this.passwordVisible = false,
      this.rightWidget,
      this.height,
      this.width,
      Key? key})
      : super(key: key);

  @override
  State<EditRounded2> createState() => _EditRounded2State();
}

class _EditRounded2State extends State<EditRounded2> {
  ///控制密码显示隐藏对应图标
  bool passwordVisible = false;
  late TextEditingController? controller;
  late OutlineInputBorder customBorder;

  ///删除按钮是否显示 true显示
  bool isShownClean = false;

  @override
  void initState() {
    passwordVisible = widget.passwordVisible;
    controller = widget.controller ?? TextEditingController();
    customBorder = OutlineInputBorder(
        borderRadius: BorderRadius.circular(widget.radius ?? 10),
        borderSide: BorderSide.none);
    controller?.addListener(() {
      int length = controller?.text.length ?? 0;
      if (length == 0 && isShownClean) {
        setState(() {
          isShownClean = false;
        });
      }
      if (length > 0 && !isShownClean) {
        setState(() {
          isShownClean = true;
        });
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        height: widget.height ?? 44,
        width: widget.width ?? double.infinity,
        padding: const EdgeInsets.only(right: 10),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            color: const Color(0xff323440)),
        child: IntrinsicHeight(
          child: Row(
            children: [
              Expanded(
                child: TextField(
                  style: const TextStyle(color: Colors.white, fontSize: 14),
                  obscureText: passwordVisible,
                  inputFormatters: widget.inputFormatters,
                  keyboardType: widget.keyboardType,
                  maxLines: 1,
                  controller: controller,
                  decoration: InputDecoration(
                    border: customBorder,
                    enabledBorder: customBorder,
                    focusedBorder: customBorder,
                    focusedErrorBorder: customBorder,
                    errorBorder: customBorder,
                    // errorText: widget.errorText,
                    // filled: true,
                    // fillColor: const Color(0xff323440),
                    hintText: widget.hint ?? '',
                    hintStyle:
                        const TextStyle(color: Color(0xff6C6F78), fontSize: 14),
                    contentPadding: const EdgeInsets.only(left: 16, right: 0),
                  ),
                  textAlign: TextAlign.start,
                ),
              ),

              //删除按钮
              Visibility(
                visible: (widget.controller?.text.length ?? 0) > 0,
                child: InkWell(
                  child: const Padding(
                    padding: EdgeInsets.only(left: 5, right: 5),
                    child: Icon(
                      Icons.cancel_sharp,
                      color: Colors.white,
                      size: 16,
                    ),
                  ),
                  onTap: () {
                    setState(() {
                      controller?.clear();
                    });
                  },
                ),
              ),

              //显示隐藏密码
              Visibility(
                visible: widget.passwordVisible,
                child: InkWell(
                  child: Padding(
                    padding: const EdgeInsets.only(right: 5),
                    child: Icon(
                      passwordVisible ? Icons.visibility_off : Icons.visibility,
                      size: 16,
                    ),
                  ),
                  onTap: () {
                    setState(() {
                      passwordVisible = !passwordVisible;
                    });
                  },
                ),
              ),
              widget.rightWidget ??
                  const SizedBox(
                    width: 0,
                    height: 0,
                  )
            ],
          ),
        ));
  }
}
